package com.colin.videoservice.controller;

import cn.hutool.json.JSONObject;
import com.colin.bean.Video;
import com.colin.bean.VideoCollect;
import com.colin.bean.VideoLike;
import com.colin.response.R;
import com.colin.videoservice.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/video")
public class VideoController {

    @Autowired
    private VideoService videoService;


    @GetMapping("/{videoId}")
    public R<Video> getVideoById(@PathVariable("videoId") Long videoId) {
        return videoService.getVideoPath(videoId);
    }

    @PostMapping
    public R<Boolean> initVideo() {
        return videoService.initVideo();
    }

    @PostMapping("/publish/videoFile")
    public R<Boolean> publishVideoFile(@RequestParam("videoFile") MultipartFile videoFile) {
        return videoService.uploadVideo(videoFile);
    }

    @PostMapping("/publish/coverFile")
    public R<Boolean> publishCoverFile(@RequestParam("coverFile") MultipartFile coverFile) {
        return videoService.uploadCover(coverFile);
    }

    @PostMapping("/publish/videoDetail")
    public R<Boolean> publishVideoDetail(@RequestBody Video videoDetail) {
        return videoService.uploadVideoDetail(videoDetail);
    }

    @DeleteMapping("/{videoId}")
    public R<Boolean> deleteVideoById(@PathVariable("videoId") Long videoId) {
        return videoService.deleteVideo(videoId);
    }

    @PostMapping("/like")
    public R<Boolean> likeVideo(@RequestBody JSONObject jsonObject) {
        String videoId = (String) jsonObject.get("videoId");
        return videoService.likeVideo(videoId);
    }

    @GetMapping("/like")
    public R<List<Video>> getLikeVideoList() {
        return videoService.getLikeVideoList();
    }

    @DeleteMapping("/like")
    public R<Boolean> doDisLikeVideo(@RequestBody JSONObject jsonObject) {
        String videoId = (String) jsonObject.get("videoId");
        return videoService.doDisLikeVideo(videoId);
    }

    @PostMapping("/collect")
    public R<Boolean> doCollectVideo(@RequestBody JSONObject jsonObject) {
        String videoId = (String) jsonObject.get("videoId");
        return videoService.collectVideo(videoId);
    }

    @GetMapping("/collect")
    public R<List<Video>> getCollectVideoList() {
        return videoService.getCollectVideoList();
    }

    @DeleteMapping("/collect")
    public R<Boolean> doUnCollectVideo(@RequestBody JSONObject jsonObject) {
        String videoId = (String) jsonObject.get("videoId");
        return videoService.doUnCollectVideo(videoId);
    }

    @GetMapping("/like/status")
    public R<VideoLike> getLikeStatus(@RequestBody JSONObject jsonObject) {
        String videoId = (String) jsonObject.get("videoId");
        return videoService.getLikeStatus(videoId);
    }

    @GetMapping("/collect/status")
    public R<VideoCollect> getCollectStatus(@RequestBody JSONObject jsonObject) {
        String videoId = (String) jsonObject.get("videoId");
        return videoService.getCollectStatus(videoId);
    }

    @GetMapping("/search")
    public R<List<Video>> searchVideo(@RequestBody JSONObject jsonObject) {
        String keyword = jsonObject.getStr("keyword");
        return videoService.searchVideoList(keyword);
    }
}
